package com.zhang.novel.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConditionalOnProperty(prefix = "spring.rabbitmq",name = "enable",havingValue = "1")
public class RabbitMqConfig {

    /**
     * 创建更新数据库的队列
     * @return
     */
    @Bean
    public Queue updateDbQueue(){
        return new Queue("UPDATE-DB-QUEUE",true);
    }

    /**
     * 创建增加点击量的广播类型的交换机
     * @return
     */
    @Bean
    public FanoutExchange addVisitCountExchange(){
        return new FanoutExchange("ADD-VISIT-COUNT-EXCHANGE");
    }

    /**
     * 将更新数据库的队列与增加点击量的交换机进行绑定
     * @return
     */
    @Bean
    public Binding bingingUpdateDb(){
        return BindingBuilder.bind(updateDbQueue()).to(addVisitCountExchange());
    }
}
